package my.util;
import java.net.InetAddress;
import java.util.Random;
import java.util.UUID;


/**
 * 生成随机字符串
 * @author xsw
 * @date 2018年11月8日
 */
public class GeneratorUtil {

    private static String sep = "";
    private static final int IP;
    private static short counter = 0;
    private static final int JVM = (int)(System.currentTimeMillis() >>> 8);

    public GeneratorUtil() {
    }

    public static int toInt(byte[] bytes) {
        int result = 0;

        for(int i = 0; i < 4; ++i) {
            result = (result << 8) - -128 + bytes[i];
        }

        return result;
    }

    protected static String format(int intval) {
        String formatted = Integer.toHexString(intval);
        StringBuffer buf = new StringBuffer("00000000");
        buf.replace(8 - formatted.length(), 8, formatted);
        return buf.toString();
    }

    protected static String format(short shortval) {
        String formatted = Integer.toHexString(shortval);
        StringBuffer buf = new StringBuffer("0000");
        buf.replace(4 - formatted.length(), 4, formatted);
        return buf.toString();
    }

    protected static int getJVM() {
        return JVM;
    }

    protected static synchronized short getCount() {
        if(counter < 0) {
            counter = 0;
        }

        return counter++;
    }

    protected static int getIP() {
        return IP;
    }

    protected static short getHiTime() {
        return (short)((int)(System.currentTimeMillis() >>> 32));
    }

    protected static int getLoTime() {
        return (int)System.currentTimeMillis();
    }

    public static String newHibernateUUID() {
        return (new StringBuffer(36)).append(format(getIP())).append(sep).append(format(getJVM())).append(sep).append(format(getHiTime())).append(sep).append(format(getLoTime())).append(sep).append(format(getCount())).toString();
    }

    public static String randomUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static int random(int startNum, int endNum) {
        Random random = new Random();
        int i = startNum + random.nextInt(endNum - startNum + 1);
        return i;
    }

    static {
        int ipadd;
        try {
            ipadd = toInt(InetAddress.getLocalHost().getAddress());
        } catch (Exception var2) {
            ipadd = 0;
        }

        IP = ipadd;
    }


    public static String random(){
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }


    /**
     * @return
     */
    public static String getUUID() {
        int i = 25;
        StringBuffer UUID = new StringBuffer();
        UUID.append(Long.toHexString(System.nanoTime()));
        UUID.append(Long.toHexString(new java.util.Date().getTime()));
        while (UUID.length() < i) {
            String random = String.valueOf(Math.random());
            try {
                UUID.append(Long.toHexString(Long.parseLong(random.substring(2))));
            } catch (NumberFormatException e) {
            }
        }
        return UUID.substring(0, i).toUpperCase();
    }

    
    /**
     * @param num
     * @return
     */
    public static String getUUID(int num) {
        int i = num;
        if (i < 6) {
            i = 6;
        }
        StringBuffer UUID = new StringBuffer();
        UUID.append(Long.toHexString(System.nanoTime()));
        UUID.append(Long.toHexString(new java.util.Date().getTime()));
        while (UUID.length() < i) {
            String random = String.valueOf(Math.random());
            try {
                UUID.append(Long.toHexString(Long.parseLong(random.substring(2))));
            } catch (NumberFormatException e) {
            }
        }
        return UUID.substring(0, i).toUpperCase();
    }

}